package com.sfeir.poc.portal.gui.client.widgets;

import com.google.gwt.user.client.ui.HTMLPanel;
import com.sfeir.poc.portal.gui.client.objects.WidgetDescription;
import com.sfeir.poc.portal.gui.client.services.feed.Entrie;

/**
 * Widget that displays Rss news as html
 * Should be improved by building a GWT interface
 * Possibility to add pagination, ...
 * @author bostoen.v
 *
 */
public class RssWidget extends AbstractWidget {
	private HTMLPanel newsPanel = null;

	public RssWidget(WidgetDescription widgetDescription){
		super();
		newsPanel = new HTMLPanel(initContent(widgetDescription));
		newsPanel.setWidth("100%");
		newsPanel.setHeight("100%");
		widgetContainer.add(newsPanel);
	}

	private String initContent(WidgetDescription widgetDescription){
		String result = "<table width=\"100%\">";
		int end = widgetDescription.getFeed().getEntries().size();
		if (end > new Integer(widgetDescription.getArgs().get(0))) {
			end = new Integer(widgetDescription.getArgs().get(0));
		}
		Entrie entrie = null;
		for (int i = 0; i < end; i++) {
			entrie = (Entrie) widgetDescription.getFeed().getEntries().get(i);
			result += "<tr><td class=\"news\"><small style=\"color: rgb(170, 170, 170);\">" + entrie.getPublishedDate() + "</small></td>";
			result += "<td class=\"news\"><a href=\"" + entrie.getLink() + "\" target=\"_blank\">" + entrie.getTitle() + "</a></td></tr>";
		}
		result += "</table>";
		return result;
	}
}
